package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.databinding.ObservableArrayList;
import android.view.View;
import android.widget.Toast;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.PopupActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * Created by zxh on 16/11/25.
 */

public class MyAccountModel extends XListModel {
    //    private ListBindingAdapter detail;
    private double balance;
    private String rechargeValue;

    @Bindable
    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
        notifyPropertyChanged(BR.balance);
    }

    @Bindable
    public String getRechargeValue() {
        return rechargeValue;
    }

    public void setRechargeValue(String rechargeValue) {
        this.rechargeValue = rechargeValue;
        notifyPropertyChanged(BR.rechargeValue);
    }

//    @Bindable
//    public ListBindingAdapter getDetail() {
//        return detail;
//    }

    public MyAccountModel(Context context) {
        super(context);
        super.getHandler().setTitle("我的账户");
//        detail = new ListBindingAdapter<BalanceItem>(context, BR.balanceItem);
        getAdapter().setVariableId(BR.balanceItem);
        rechargeValue = "10.00";
//        this.load();
    }

    /**
     * 点击充值，打开充值界面
     *
     * @param view
     */
    public void onClickRecharge(View view) {
        CommonFragment fragment = new CommonFragment();
        fragment.setVariableId(BR.account);
        fragment.setLayoutId(R.layout.f_recharge);
        fragment.setDataModel(this);
        HomeActivity.switchFragment((Activity) getContext(), fragment);
    }

    /**
     * 点击提现，打开提现界面
     *
     * @param view
     */
    public void onClickWithdraw(View view) {
        WithdrawModel model = new WithdrawModel(getContext());
        model.setBalance(balance);
        CommonFragment fragment = new CommonFragment();
        fragment.setVariableId(BR.account);
        fragment.setLayoutId(R.layout.f_withdraw);
        fragment.setDataModel(model);
        HomeActivity.switchFragment((Activity) getContext(), fragment);
    }

    /**
     * 点击充值按钮，显示选择充值方式界面
     *
     * @param view
     */
    public void onClickRechargeButton(View view) {
        try {
            Double value = Double.valueOf(rechargeValue);
            if (value < 10) {
                showToast("充值金额不得低于10.00元");
            } else {
                Activity activity = (Activity) getContext();
                PayModel model = new PayModel(getContext(), value);
                model.setTitle("账户充值");
                model.setPayCallback(new PayModel.Callback() {
                    @Override
                    public boolean onPay(PayItem payType) {
                        // 用户选择支付方式后回掉此方法
                        // payType.getType()获取支付方式，PayModel.PAY_ALI表示支付宝付款、
                        // PayModel.PAY_WX表示微信付款
                        // 返回true关闭选择支付方式弹窗
                        // 打开的新窗口的返回结果请在本类的onActivityResult中处理
                        return false;
                    }
                });
                PopupActivity.startActivityForResult(activity, R.layout.v_dialog_advice_pay,
                        HomeActivity.REQUEST_PAY, com.android.databinding.library.baseAdapters.BR.payModel, model);
            }
        } catch (Exception ex) {
            showToast(ex.getMessage());
            return;
        }

    }

    @Override
    public void load(final int page) {
        final Activity activity = (Activity) getContext();
        LoadingDialog.getInstance(activity).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                setBalance(WebApi.getBalance(activity));
                ArrayList list = WebApi.getBalanceDetail(activity, page);
                try {
                    for (Object item : list) {
                        LinkedTreeMap map = (LinkedTreeMap) item;
                        BalanceItem balanceItem = new BalanceItem(activity);

                        balanceItem.setDate(Utils.getDateFromTimestamp(map.get("systime")));
                        balanceItem.setType((String) map.get("remark"));
                        balanceItem.setValue(Utils.getDoubleValue(map.get("amount")));
                        getAdapter().add(balanceItem);
                    }
                    if(list.size() > 0){
                        setCurrentPage(page);
                    }
                    notifyDataSetChanged(list);
                } catch (Exception ex) {
                    showToast("解析数据时发生错误");
                }
                LoadingDialog.getInstance(activity).dismiss();
            }
        }).start();
    }
}
